'----------------------------------------------
' Name: Ali Karbassi & Mike Raasch
' Date: April 10, 2007
' Description: Project 6
'              Class MultiChoice
'----------------------------------------------
Public Class MultiChoice
	Implements IQuestion

	' Private fields
	Private _index As Integer
   Private _key As Integer
	Private _mode As QuestionMode

   ''' <summary>
   ''' The height of the control
   ''' </summary>
   ''' <value></value>
   ''' <returns></returns>
   ''' <remarks></remarks>
   Private Overloads ReadOnly Property height() As Integer Implements IQuestion.height
      Get
         Return Me.ClientSize.Height
      End Get
   End Property

   ''' <summary>
   ''' The width of the control
   ''' </summary>
   ''' <value></value>
   ''' <returns></returns>
   ''' <remarks></remarks>
   Private Overloads ReadOnly Property width() As Integer Implements IQuestion.width
      Get
         Return Me.ClientSize.Width
      End Get
   End Property

   ''' <summary>
   ''' Sets or Reads the question index, which can be stored as the Text of lblIndex.
   ''' </summary>
   ''' <value></value>
   ''' <returns></returns>
   ''' <remarks></remarks>
   Private Overloads Property Index() As Integer Implements IQuestion.Index
      Get
         Return _index
      End Get
      Set(ByVal value As Integer)
         _index = value
      End Set
   End Property

   ''' <summary>
   ''' Use private methods to implement interface
   ''' </summary>
   ''' <remarks></remarks>
	Private Event Changed() Implements IQuestion.Changed

   ''' <summary>
   ''' Removes the question from theForm and then decreases the height of theForm by the height of the 
   ''' control.
   ''' </summary>
   ''' <param name="theForm"></param>
   ''' <remarks></remarks>
   Private Sub Remove(ByVal theForm As System.Windows.Forms.Form) Implements IQuestion.Remove

      theForm.Controls.Remove(Me)
      theForm.Height -= Me.height

   End Sub

   ''' <summary>
   ''' Checks if the question is completed and throws an exception if not completed. Saves the key for 
   ''' TrueFalse and MultiChoice.
   ''' </summary>
   ''' <remarks></remarks>
   Private Sub Save() Implements IQuestion.Save

      If txtQuestion.Text.Trim.Length <= 0 Then

         Throw New Exception("No question is entered.")

      ElseIf lblIndex.Text.Trim.Length <= 0 Then

         Throw New Exception("No index is entered.")

      ElseIf txtChoice1.Text.Trim.Length <= 0 Or txtChoice2.Text.Trim.Length <= 0 Or _
             txtChoice3.Text.Trim.Length <= 0 Or txtChoice4.Text.Trim.Length <= 0 Then

         Throw New Exception("Choices are not entered.")

      End If

      If optChoice1.Checked Then
         _key = 1
      ElseIf optChoice2.Checked Then
         _key = 2
      ElseIf optChoice3.Checked Then
         _key = 3
      ElseIf optChoice4.Checked Then
         _key = 4
      Else
         _key = 0
      End If

   End Sub

   ''' <summary>
   ''' Returns 1 if correct, 0 otherwise (including no answer)
   ''' </summary>
   ''' <returns></returns>
   ''' <remarks>Apparently in VB, True is -1 and False is 0, not True: 1, False:0</remarks>
   Private Function Score() As Integer Implements IQuestion.Score

      If _mode = QuestionMode.Taking Then

         Dim scoreKey As Integer

         If optChoice1.Checked Then
            scoreKey = 1
         ElseIf optChoice2.Checked Then
            scoreKey = 2
         ElseIf optChoice3.Checked Then
            scoreKey = 3
         ElseIf optChoice4.Checked Then
            scoreKey = 4
         Else
            scoreKey = 0
         End If

         Return Math.Abs(CType(scoreKey = _key, Integer))

      End If

   End Function

   ''' <summary>
   ''' Displays a question at point Pos of theForm and then increases the height of theForm by the height of 
   ''' the control.
   ''' 
   ''' When theMode is Creating
   '''    TextBox for the question should be Read/Write
   '''    Key should be displayed
   '''    Choice textboxes of MultiCHoice class should be Read/Write
   '''    txtComment of Comment class should be ReadOnly
   ''' 
   ''' When theMode is Taking
   '''    TextBox for the question should be ReadOnly
   '''    Key should NOT be displayed
   '''    Choice textboxes for MultiCHoice class should be ReadOnly
   '''    txtComment for Comment class should be Read/Write
   ''' 
   ''' Set a private field _mode to theMode
   ''' </summary>
   ''' <param name="Pos"></param>
   ''' <param name="theForm"></param>
   ''' <param name="theMode"></param>
   ''' <remarks></remarks>
   Private Sub Display(ByVal Pos As System.Drawing.Point, ByVal theForm As System.Windows.Forms.Form, _
               ByVal theMode As QuestionMode) Implements IQuestion.Display

      theForm.Controls.Add(Me)
      Me.Location = Pos
      theForm.Height += Me.height

      If theMode = QuestionMode.Creating Then

         txtQuestion.ReadOnly = False

      Else

         optChoice1.Checked = False
         optChoice2.Checked = False
         optChoice3.Checked = False
         optChoice4.Checked = False

         txtQuestion.ReadOnly = True
         txtChoice1.ReadOnly = True
         txtChoice2.ReadOnly = True
         txtChoice3.ReadOnly = True
         txtChoice4.ReadOnly = True

         txtQuestion.BackColor = Color.White
         txtChoice1.BackColor = Color.White
         txtChoice2.BackColor = Color.White
         txtChoice3.BackColor = Color.White
         txtChoice4.BackColor = Color.White

      End If

      _mode = theMode
      txtQuestion.Focus()

      lblIndex.Text = _index & "."

   End Sub

   ''' <summary>
   ''' One method handle event of multiple controls
   ''' </summary>
   ''' <param name="sender"></param>
   ''' <param name="e"></param>
   ''' <remarks></remarks>
   Private Sub optChoice1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
               Handles optChoice1.CheckedChanged, optChoice2.CheckedChanged, optChoice3.CheckedChanged, _
                     optChoice4.CheckedChanged, txtChoice1.TextChanged, txtChoice2.TextChanged, _
                     txtChoice3.TextChanged, txtChoice4.TextChanged, txtQuestion.TextChanged

      If _mode = QuestionMode.Creating Then
         RaiseEvent Changed()
      End If

   End Sub

End Class
